//
//  AY_BookVpFootView.m
//  AiYue
//
//  Created by boWen on 2023/10/30.
//

#import "AY_BookVpFootView.h"
#import "UIImage+GIF.h"

@interface AY_BookVpFootView ()
@property (nonatomic,strong) UIView * bg_View;//底部圆角
@property (nonatomic,strong) UIView * typeView;
@property (nonatomic,strong) UILabel * typeInfo1;
@property (nonatomic,strong) UIImageView * zfbImg;
@property (nonatomic,strong) UILabel * typeInfo2;
@property (nonatomic,strong) UIImageView * arrow;
@property (nonatomic,strong) UIImageView * loadingView;
@property (strong,nonatomic) UIImageView *lineView;
@end


@implementation AY_BookVpFootView

- (instancetype)initWithFrame:(CGRect)frame {
    if([super initWithFrame:frame]) {
        [self addAllSubviewsWithUI];
    }
    return self;
}

- (void)initTimer {
//    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(setTimer) userInfo:nil repeats:YES];
}

- (void)addAllSubviewsWithUI {
    self.backgroundColor = [UIColor whiteColor];
    
    self.bg_View = [AYTool createViewWithFrame:CGRectZero andBg:[UIColor whiteColor] andRadius:YNUM(12) andAddSubView:self];
    self.bg_View.layer.masksToBounds = NO;
    self.bg_View.layer.shadowColor = [UIColor lightGrayColor].CGColor; // 阴影颜色
    self.bg_View.layer.shadowOffset = CGSizeMake(0, -1); // 偏移距离
    self.bg_View.layer.shadowOpacity = 0.2; // 不透明度
    self.bg_View.layer.shadowRadius = 5.0; // 半径
    [self.bg_View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self).offset(YNUM(15));
        make.height.mas_offset(GK_TABBAR_HEIGHT + YNUM(40));
    }];
    
    self.typeView = [AYTool createViewWithFrame:CGRectZero andBg:nil andRadius:0 andAddSubView:self.bg_View];
    [self.typeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.bg_View);
        make.height.mas_equalTo(YNUM(30));
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(alertWithPyyAction)];
    [self.typeView addGestureRecognizer:tap];
    
    self.typeInfo2 = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#814B1D") andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(12)] andLines:1 andRadius:0 andAddSubView:self.typeView];
    [self.typeInfo2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.typeView);
        make.centerX.equalTo(self.typeView).offset(YNUM(20));
    }];
    
    self.arrow = [AYTool createImageViewWithFrame:CGRectZero andImage:YImage(@"AY_mvp_arrow") andBg:nil andRadius:0 andAddSubView:self.typeView];
    [self.arrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.typeInfo2.mas_right);
        make.centerY.equalTo(self.typeView);
        make.width.height.mas_equalTo(YNUM(14));
    }];
    
    self.zfbImg = [AYTool createImageViewWithFrame:CGRectZero andImage:YImage(@"icon_ali_chase") andBg:nil andRadius:0 andAddSubView:self.typeView];
    [self.zfbImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.typeView);
        make.right.equalTo(self.typeInfo2.mas_left);
        make.width.height.mas_equalTo(YNUM(14));
    }];
    
    self.typeInfo1 = [AYTool createLabelWithFrame:CGRectZero andText:@"使用 " andTextColor:COLOR(@"#814B1D") andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(12)] andLines:1 andRadius:0 andAddSubView:self.typeView];
    [self.typeInfo1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.typeView);
        make.right.equalTo(self.zfbImg.mas_left);
    }];
    
    self.ktBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:nil andFont:nil andTitleColor:COLOR(@"#814B1D") andBg:COLOR(@"#f7dd99") andImage:nil andBgImage:nil andTarget:self andAction:@selector(ktAction) andRadius:YNUM(8) andAddSubView:self.bg_View];
    self.ktBtn.layer.borderWidth = 1;
    self.ktBtn.titleLabel.font = [UIFont boldSystemFontOfSize:YNUM(16)];
    self.ktBtn.layer.borderColor = COLOR(@"e7d5a5").CGColor;
    [self.ktBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bg_View).offset(YNUM(16));
        make.right.equalTo(self.bg_View).offset(-YNUM(16));
        make.top.equalTo(self.typeView.mas_bottom);
        make.height.mas_equalTo(YNUM(46));
    }];
    [self.ktBtn setBackgroundImage:[AYTool buttonImageFromColor:COLOR(@"e7d5a5")] forState:UIControlStateHighlighted];
    
    [self layoutIfNeeded];
    self.lineView = [AYTool createImageViewWithFrame:CGRectMake(-70, 0, 60, self.ktBtn.mj_h) andImage:YImage(@"AY_detail_line") andBg:nil andRadius:0 andAddSubView:self.ktBtn];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(setTimer) userInfo:nil repeats:YES];
    
    self.loadingBack = [AYTool createViewWithFrame:CGRectZero andBg:COLOR(@"#E7D5A5") andRadius:YNUM(8) andAddSubView:self.bg_View];
    self.loadingBack.hidden = YES;
    [self.loadingBack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bg_View).offset(YNUM(16));
        make.right.equalTo(self.bg_View).offset(-YNUM(16));
        make.top.equalTo(self.typeView.mas_bottom);
        make.height.mas_equalTo(YNUM(46));
    }];
    
    NSString * path = [[NSBundle mainBundle] pathForResource:@"AY_mvp_loading" ofType:@"gif"];
    NSData * data = [NSData dataWithContentsOfFile:path];
    UIImage *image = [UIImage sd_imageWithGIFData:data];
    self.loadingView = [AYTool createImageViewWithFrame:CGRectZero andImage:image andBg:nil andRadius:0 andAddSubView:self.loadingBack];
    [self.loadingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.equalTo(self.ktBtn);
        make.width.mas_equalTo(YNUM(40));
        make.height.mas_equalTo(YNUM(10));
    }];
    
}

- (void)setTimer {
    [UIView animateWithDuration:2 animations:^{
        self.lineView.frame = CGRectMake(self.ktBtn.mj_w, 0, 60, self.ktBtn.mj_h);
    } completion:^(BOOL finished) {
        self.lineView.frame = CGRectMake(-70, 0, 60, self.ktBtn.mj_h);
    }];
}

- (void)ktAction {
    if ([self.delegate respondsToSelector:@selector(ktAction)]) {
        [self.delegate ktAction];
    }
}

- (void)sendVpProductModel:(VpProductModel *)vpModel andPType:(NSString *)pType {
    _vpModel = vpModel;
    [self.ktBtn setTitle:[NSString stringWithFormat:@"%@并%@￥%@",[AYMMKV getStringForKey:KT],[AYMMKV getStringForKey:ZF],_vpModel.price] forState:UIControlStateNormal];
    if ([pType isEqual:@"ZFB"]) {
        self.typeInfo2.text = [NSString stringWithFormat:@" %@%@，%@",[AYMMKV getStringForKey:ZFB],[AYMMKV getStringForKey:ZF],vpModel.giveTipStr];
        self.zfbImg.image = YImage(@"icon_ali_chase");
    }else {
        self.typeInfo2.text = [NSString stringWithFormat:@" %@%@，暂无优惠",[AYMMKV getStringForKey:WXSTR],[AYMMKV getStringForKey:ZF]];
        self.zfbImg.image = YImage(@"icon_weC_chase");
    }
}

- (void)alertWithPyyAction {
    if(self.kaitBlock) {
        self.kaitBlock();
    }
}

@end
